package cn.framelife.rainbow.ui.dialog;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.GridData;

import cn.framelife.rainbow.ui.entity.Module;


/**
 * New Module Dialog
 * 
 * @author 潘广伟  p_3er@qq.com 
 * @date 2013年11月21日 下午3:41:15 
 *
 */
public class NewModuleDialog extends Dialog {
	private Text nameText;
	private Text menuText;
	
	private Module module;
	
	
	public Module getModule() {
		return module;
	}

	public NewModuleDialog(Shell shell) {
		super(shell);
	}

	/**
	 * Create contents of the dialog.
	 * @param parent
	 */
	@Override
	protected Control createDialogArea(Composite parent) {
		this.getShell().setText("Create a new module");
		Composite container = initUI(parent);

		return container;
	}

	private Composite initUI(Composite parent) {
		Composite container = new Composite(parent, SWT.NULL);
		container.setLayout(new GridLayout(3, false));
		

		Label nameLabel = new Label(container, SWT.NONE);
		nameLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, true, false, 1, 1));
		nameLabel.setText("Module name: ");
		
		GridData gd_srcFolderField = new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1);
		gd_srcFolderField.widthHint = 200;
		nameText = new Text(container, SWT.BORDER);
		nameText.setLayoutData(gd_srcFolderField);
		
		Label menuLabel = new Label(container, SWT.NONE);
		menuLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, true, false, 1, 1));
		menuLabel.setText("Menu name: ");
		
		menuText = new Text(container, SWT.BORDER);
		menuText.setLayoutData(gd_srcFolderField);
		
		return container;
	}

    /**
     * dialog button pressed
     */
	protected void buttonPressed(int buttonId) {
		if (buttonId == IDialogConstants.OK_ID) {
			module = new Module();
			module.setModuleName(nameText.getText());
			module.setMenuName(menuText.getText());
		}
		super.buttonPressed(buttonId);
	}

	/**
	 * Create contents of the button bar.
	 * @param parent
	 */
	@Override
	protected void createButtonsForButtonBar(Composite parent) {
		createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL,
				true);
		createButton(parent, IDialogConstants.CANCEL_ID,
				IDialogConstants.CANCEL_LABEL, false);
	}

	/**
	 * Return the initial size of the dialog.
	 */
	@Override
	protected Point getInitialSize() {
		return new Point(400, 150);
	}

}
